import { request } from '../../request/index'

// pages/goods_list/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
     tabs:[
       {
         id:0,
         value:'综合',
         isActive:true
       },
       {
        id:1,
        value:'销量',
        isActive:false
       },
       {
        id:2,
        value:'价格',
        isActive:false
       }
     ],
     goods_list:[]
  },
  QueryParams: {
     query:'',
     cid: '',
     pagenum:1,
     pagesize:10
  },
  // 总页数
  totalpages:1,
  // 标题点击事件 子组件传递来
  l(e) {
   console.log(e)
  //  获取被点击的标题索引
   const {index} = e.detail
  //  修改原数组
   let {tabs} = this.data
   console.log(tabs)
   tabs.forEach((v,i)=> i===index? v.isActive=true : v.isActive=false)
   this.setData({
    tabs
   })
 },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.QueryParams.cid = options.cid
   console.log(options)
   this.getGoodsList()
  },
  // 获取商品列表数据
  async getGoodsList() {
     const res = await request({url:'/goods/search',data:this.QueryParams})
     const total = res.data.message.total
     this.totalpages = Math.ceil(total/this.QueryParams.pagesize)
     console.log(this.totalpages)
     this.setData({
       goods_list: [...this.data.goods_list,...res.data.message.goods]
      })
      wx.stopPullDownRefresh()
      console.log(res)
      console.log(this.data.goods_list)
  },
  // 页面上滑 滚动条触底事件
  onReachBottom() {
      // console.log('页面触底了')
      if(this.QueryParams.pagenum >= this.totalpages) {
          wx.showToast({
            title: '没有数据了',
            duration:1000,
            icon:'none'
          })
      }else {
        this.QueryParams.pagenum++
         this.getGoodsList()
      }
  },
  // 下拉刷新事件
  onPullDownRefresh() {
     this.setData({
       goodsList:[]
     })
    //  重置页码
    this.QueryParams.pagenum = 1
    // 重新发送请求
    this.getGoodsList()
  }
})